<?php
/**************************************
 *
 * @services 服务层~主要进行数据筛选与过滤
 *
 **************************************/
namespace app\common\services;

use GuzzleHttp\Client;
use think\config;
use think\Env;
use  think\Cache;
use think\Validate;
use think\Db;
use think\Model;

class FligtServices
{
    //机票订购
    public function getFligt($respons)
    {
        $data['trip'] = $respons['trip'];
        $data['return_trip'] = $respons['return_trip'];
        //航班号
        $rsl['fligt'] = Db::name('flight')->field(['tr_flight as flightNum,retr_flight'])->where($data)->group('tr_flight')->select();
        $info= Db::name('flight')
            ->field([
                'id',
                'trip as startCity',
                'return_trip as endCity',
                'tr_flight as flyNum',
                'trip_time',
                'tto_time',
                'rt_time',
                'rto_time',
                'price',
                'round_days',
                'stock',
                'retr_flight',
                'tr_flight'
            ])
            ->where($data)->select();
        //航班行程
        foreach ($info as $k=>$v) {
            $info[$k]['trip_time'] = date("m月d日",$v['trip_time']);
            $info[$k]['tto_time'] = date("H:i",$v['tto_time']);
            $info[$k]['rt_time'] = date("m月d日",$v['rt_time']);
            $info[$k]['rto_time'] = date("H:i",$v['rto_time']);

        }
        $rsl['fligtList'] = $info;
        return $rsl;
    }

    //航班动态查询
    public function getFligtList($respons)
    {
        $data['tr_flight'] = $respons['tr_flight'];
        $data['trip'] = $respons['trip'];
        $data['return_trip'] = $respons['return_trip'];
        $info= Db::name('flight')
            ->field([
                'id',
                'trip as startCity',
                'return_trip as endCity',
                'tr_flight as flyNum',
                'trip_time',
                'tto_time',
                'rt_time',
                'rto_time',
                'price',
                'round_days',
                'stock',
                'retr_flight',
                'tr_flight'
            ])
            ->where($data)->select();
        //航班行程
        foreach ($info as $k=>$v) {
            $info[$k]['trip_time'] = date("m月d日",$v['trip_time']);
            $info[$k]['tto_time'] = date("H:i",$v['tto_time']);
            $info[$k]['rt_time'] = date("m月d日",$v['rt_time']);
            $info[$k]['rto_time'] = date("H:i",$v['rto_time']);

        }
        return $info;
    }

    public function getFligtInfo($respons)
    {
        $data['id'] = $respons['id'];
        return Db::name('flight')->where($data)->find();
    }

//    public function getFligtList($respons)
//    {
//        $data['trip'] = $respons['trip'];
//        return Db::name('flight')->where($data)->select();
//    }

    public function getFligtTrip()
    {

        $rsl['trip'] =  Db::name('flight')->column("DISTINCT trip as trip");
        $rsl['return_trip'] = Db::name('flight')->column("DISTINCT return_trip as return_trip");
        return $rsl;
    }

    public function addVirtualOrder($respons)
    {
        $time = date('Y-m-d', time());
        $a = Cache::get($time);
        if (!$a) {
            Cache::set($time, 1, 3600 * 24);
        }else{
            Cache::inc($time);
        }
        $respons['create_time'] = time();
        $respons['trip_time'] = strtotime($respons['trip_time']);
        $respons['rt_time'] = strtotime($respons['rt_time']);
        $rsl = Db::name('virtual_order')->insertGetId($respons);
        if ($rsl) {
            return $rsl;
        }
        return false;
    }
}